﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Project1.Interface;
using Project1.Common;
using HalconDotNet;
using System.Windows;
namespace Project1.Element
{
    public class PL : IComplexMes
    {
        private int type;
        public delegate void drawFunc(string type);
        private PointPack pointPack = null;
        private LinePack linePack = null;
        public int Type { get { return type; } set { type = value; } }
        private Output output;
        private string name;
        private static int[] existCount = new int[10];
        private double supOffsetRow, supOffsetColumn;
        private SupportLine supportLine;

        private List<string> nameList;


        public List<string> NameList
        {
            get { return nameList; }
            set
            {

                foreach (var item in value)
                {
                    nameList.Add(item);
                }
            }
        }
        public void Unload()
        {
            pointPack.Unload();
            linePack.Unload();
        }

        public void ApplyChange(string mode)
        {

            switch (type)
            {
                case ConstValue.MEASURE_PL_DIST:
                    if(mode == "add")name = "plDist" + existCount[0]++;
                    supOffsetRow = supportLine.RowOffset;
                    supOffsetColumn = supportLine.ColumnOffset;
                    break;
                case ConstValue.MEASURE_PL_Mid:
                    if(mode=="add")name = "plMid" + existCount[1]++;
                    break;
                case ConstValue.MEASURE_PL_Parallel:
                    if (mode == "add") name = "plParallel" + existCount[2]++;
                    break;
                case ConstValue.MEASURE_PL_VERTICAL:
                    if (mode == "add") name = "plVertical" + existCount[3]++;
                    break;
                default:
                    break;
            }
        }

        public PointPack PointPack
        {
            get { return pointPack; }
            set { pointPack = value; }
        }
        public LinePack LinePack
        {
            get { return linePack; }
            set { linePack = value; }
        }
        public PL()
        {
            nameList = new List<string>();
        }

        public void Dispose()
        {
            linePack.Dispose();
            pointPack.Dispose();
            linePack = null;
            pointPack = null;
        }

        public PL(PL pl)
        {
            supOffsetRow = pl.supOffsetRow;
            supOffsetColumn = pl.supOffsetColumn;
            name = pl.name;
            nameList = new List<string>();
            type = pl.type;
            output = new Output(pl.output);
            pointPack = new PointPack(pl.pointPack);
            linePack = new LinePack(pl.linePack);
        }
        public Output Rs
        {
            set { output = value; }
            get { return output; }
        }
        public bool loadElem(List<IElemPack> ieps)
        {
            pointPack = new PointPack();
            linePack = new LinePack();
            string tips = "只能选择一个点和一条边";
            if (ieps.Count > 2)
            {
                MessageBox.Show(tips);
                return false;
            }
            for (int i = 0; i < ieps.Count; i++)
            {
                string name = ieps[i].GetType().FullName;
                if (name == ConstValue.TYPE_CIRCLEPACK)
                {
                    MessageBox.Show("混有其他基本元素");
                    return false;
                }
                else if (name == ConstValue.TYPE_LINEPACK)
                {
                    linePack = new LinePack((LinePack)ieps[i]);
                }
                else if (name == ConstValue.TYPE_POINTPACK)
                {
                    pointPack = new PointPack((PointPack)ieps[i]);
                }


            }
            ieps.Clear();
            return true;
        }

        public string Name { get { return name; } set { name = value; } }

        public void CompleteChildElem()
        {

        }


        public Output Execute(FindMethod method)
        {
            if (linePack == null || pointPack == null) throw new Exception();
       
         
            Output output = null;
            HTuple rowBegin, rowEnd, colBegin, colEnd,nr,nc,dist;
            HObject  midLineContours = null, parallelContours = null, verticalLineContours = null;
            List<Point> tempPoints = new List<Point>();
            switch (type)
            {
                case ConstValue.MEASURE_PL_DIST:
                  
                    HOperatorSet.FitLineContourXld(linePack.Contours(), "turkey", -1, 0, 5, 2.0, out rowBegin, out colBegin, out rowEnd, out colEnd, out nr, out nc, out dist);
                    HOperatorSet.DistancePl(pointPack.Row, pointPack.Column, rowBegin, colBegin, rowEnd, colEnd, out dist);
                    output = new Output((double)dist, ConstValue.DISTANCE);
                    break;
                case ConstValue.MEASURE_PL_Mid:
                   
                    GeoMethod.PL_Mid(linePack.Contours(), out midLineContours, pointPack.Row, pointPack.Column, out rowBegin, out colBegin, out rowEnd, out colEnd);
                    output = new Output(new Line(rowBegin, colBegin, rowEnd, colEnd), ConstValue.LINE);
                    break;
                case ConstValue.MEASURE_PL_Parallel:
                    GeoMethod.PL_Parallel(linePack.Contours(), pointPack.Row, pointPack.Column, out parallelContours, out rowBegin, out colBegin, out rowEnd, out colEnd);
                    output = new Output(new Line(rowBegin, colBegin, rowEnd, colEnd), ConstValue.LINE);
                    break;
                case ConstValue.MEASURE_PL_VERTICAL:
                   
                    GeoMethod.PL_VERTICAL(linePack.L.LineContours, out verticalLineContours, pointPack.Row, pointPack.Column, out rowBegin, out colBegin, out rowEnd, out colEnd);
                    output = new Output(new Line(rowBegin, colBegin, rowEnd, colEnd), ConstValue.LINE);
                    break;
                default:
                    break;
            }
            Rs = output;
            return output;

        }


        public void Execute(HObject image, HTuple homMat2D, HTuple angle, Interface.AddToList add, FindMethod find, AddSupline addSupline)
        {
            throw new NotImplementedException();
        }


        //public Output Execute(FindMethod method)
        //{
        //    if (linePack == null)
        //    {
        //        var rs = method(nameList[1], ConstValue.LINE);
        //        if (rs != null)
        //            linePack = new LinePack() { L = new Line((Line)rs) };
        //    }
        //    if (pointPack == null)
        //    {
        //        var rs = method(nameList[0], ConstValue.LINE);
        //        if (rs != null)
        //            pointPack = new PointPack() { P = new Point((Point)rs) };
        //    }
        //    Output output = null;
        //    HTuple rowBegin, rowEnd, colBegin, colEnd, nr, nc, dist;
        //    HObject midLineContours = null, parallelContours = null, verticalLineContours = null;
        //    List<Point> tempPoints = new List<Point>();
        //    switch (type)
        //    {
        //        case ConstValue.MEASURE_PL_DIST:

        //            HOperatorSet.FitLineContourXld(linePack.Contours, "turkey", -1, 0, 5, 2.0, out rowBegin, out colBegin, out rowEnd, out colEnd, out nr, out nc, out dist);
        //            HOperatorSet.DistancePl(pointPack.Row, pointPack.Column, rowBegin, colBegin, rowEnd, colEnd, out dist);
        //            output = new Output((double)dist, ConstValue.DISTANCE);
        //            break;
        //        case ConstValue.MEASURE_PL_Mid:

        //            GeoMethod.PL_Mid(linePack.Contours, out midLineContours, pointPack.Row, pointPack.Column, out rowBegin, out colBegin, out rowEnd, out colEnd);
        //            output = new Output(new Line(rowBegin, colBegin, rowEnd, colEnd), ConstValue.LINE);
        //            break;
        //        case ConstValue.MEASURE_PL_Parallel:
        //            GeoMethod.PL_Parallel(linePack.Contours, pointPack.Row, pointPack.Column, out parallelContours, out rowBegin, out colBegin, out rowEnd, out colEnd);
        //            output = new Output(new Line(rowBegin, colBegin, rowEnd, colEnd), ConstValue.LINE);
        //            break;
        //        case ConstValue.MEASURE_PL_VERTICAL:
        //            GeoMethod.PL_VERTICAL(linePack.Contours, out verticalLineContours, pointPack.Row, pointPack.Column, out rowBegin, out colBegin, out rowEnd, out colEnd);
        //            output = new Output(new Line(rowBegin, colBegin, rowEnd, colEnd), ConstValue.LINE);
        //            break;
        //        default:
        //            break;
        //    }
        //    Rs = output;
        //    return output;

        //}
    }
}
